package com.app.mymusic.utils;

import com.app.mymusic.common.Constants;
import com.app.mymusic.model.Mp3Info;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by Administrator on 2016/8/9.
 */
public class FileUtil {

    public static void write2SDCard(InputStream is,String filename)
    {
        try {
            String dir= Constants.DIR+filename;
            File file = new File(dir);
            LogUtil.showLog("cunzai:"+file.exists());
            FileOutputStream fos = new FileOutputStream(file);
            BufferedInputStream bis = new BufferedInputStream(is);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
                fos.flush();
            }
            fos.close();
            bis.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static List<Mp3Info> getMusicFromFolder(File file)
    {
        if(!file.isDirectory())
        {

        }
        file.listFiles(new Mp3FileFilter());
        return null;

    }
    static class Mp3FileFilter implements FileFilter {

        @Override
        public boolean accept(File file) {
            if(file.getName().endsWith(".mp3")||file.getName().endsWith(".m4a"))
            {
                return true;
            }
            return false;
        }
    }
}
